package it.blog.controller;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import it.blog.stub.*;


public class CategoryPostView extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5818432604886008363L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		BlogPortal bp = new BlogPortalService().getPort(BlogPortal.class);
		Integer blogId = Integer.parseInt(request.getParameter("blogID"));
		Integer categoryId = Integer.parseInt(request.getParameter("categoryID"));
		String category = request.getParameter("category");

		session.removeAttribute("mess");
		List<Post> listPostOfCategory = bp.getPostsOfCategory(categoryId,blogId);
		System.out.print("" + listPostOfCategory.size());
		
		session.setAttribute("category",category);
		session.setAttribute("categoryId",categoryId);
		session.setAttribute("listPostOfCategory",listPostOfCategory);
		
		if(listPostOfCategory==null || listPostOfCategory.size()==0){
			session.setAttribute("numPostOfCategory",0);
		}else{
			session.setAttribute("numPostOfCategory",listPostOfCategory.size());
		}
 		
		
		
		//PAGINATION
		Integer currentPage= Integer.parseInt(request.getParameter("p"));
		int itemForPage=4;
		int nPage=(int)Math.ceil(((double)listPostOfCategory.size()/itemForPage));
		int first=(currentPage-1)*itemForPage;
		int last=first+itemForPage-1;
		int vect[]={currentPage,nPage,first,last};
		session.setAttribute("paginationPost", vect);
		
		response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/home.jsp?kop=postcategory"));
	
	}
	
	

	
	
}
